<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('主机信息列表')" />
</head>
<body class="gray-bg">
	<div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="post-form">
					<div class="select-list">
						<ul>
							<li>
								节点编码：<input type="text" name="nodeCode"/>
							</li>
							<li>
								主机名：<input type="text" name="hostname"/>
							</li>
							<li>
								IP地址：<input type="text" name="ip"/>
							</li>
							<li>
								监控状态：
								<select name="status" th:with="type=${@dict.getType('enum_enable_disable')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
								</select>
							</li>
							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
							    <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>

			<div class="btn-group-sm" id="toolbar" role="group">
				<a class="btn btn-success" onclick="$.operate.add()">
	                <i class="fa fa-plus"></i> 新增
	            </a>
				<a class="btn btn-primary single disabled" onclick="$.operate.edit()">
		            <i class="fa fa-edit"></i> 修改
		        </a>
				<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()">
		            <i class="fa fa-remove"></i> 删除
		        </a>
				<a class="btn btn-warning" onclick="$.table.exportExcel()">
		            <i class="fa fa-download"></i> 导出
		        </a>
	        </div>

	        <div class="col-sm-12 select-table table-striped">
			    <table id="bootstrap-table"></table>
			</div>
		</div>
	</div>
	<th:block th:include="include :: footer" />
	<script th:inline="javascript">
		var operatingSystemDatas = [[${@dict.getType('operating_system')}]];
		var prefix = ctx + "system/node";

		$(function() {
			queryList();
		});

		function queryList() {
			var options = {
				url: prefix + "/list",
				createUrl: prefix + "/add",
				updateUrl: prefix + "/edit/{id}",
				removeUrl: prefix + "/remove",
				exportUrl: prefix + "/export",
				sortName: "nodeCode",
				modalName: "节点",
				columns: [{
					checkbox: true
				},
                {
                    field: 'id',
                    title: '编号',
                    sortable: true,
                    visible: false
                },
                {
                    field: 'nodeCode',
                    title: '节点编号',
                    sortable: true
                },
                {
                    field: 'hostname',
                    title: '主机名',
                    sortable: true
                },
                {
                    field: 'ip',
                    title: 'IP地址'
                },
                {
                    field: 'port',
                    title: '访问端口',
                    visible: false
                },
                {
                    field: 'healthCheckUrl',
                    title: '健康检查请求地址',
                    visible: false
                },
                {
                    field: 'serverInfoUrl',
                    title: '运行信息请求地址',
                    visible: false
                },
                {
                    field: 'execUrl',
                    title: '执行命令地址',
                    visible: false
                },
                {
                    field: 'isAliveUrl',
                    title: '检测软件存活的请求地址',
                    visible: false
                },
                {
                    field: 'timeSyncUrl',
                    title: '时间同步信息请求地址',
                    visible: false
                },
                {
                    field: 'operatingSystem',
                    title: '操作系统',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(operatingSystemDatas, value);
                    }
                },
				{
					field: 'sshPort',
					title: 'SSH访问端口',
					visible: false
				},
                {
                    field: 'username',
                    title: '登录用户名',
                    visible: false
                },
                {
                    field: 'startupScriptPath',
                    title: '启动监控代理脚本路径',
                    visible: false
                },
                {
                    field: 'stopScriptPath',
                    title: '停止监控代理脚本路径',
                    visible: false
                },
				{
					field: 'selfStart',
					title: '是否自启',
					align: 'center',
					sortable: true,
					formatter: function (value, row, index) {
						return statusTools(1, value, row.id);
					}
				},
                {
                    field: 'status',
                    title: '监控状态',
                    align: 'center',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return statusTools(2, value, row.id);
                    }
                },
                {
                    field: 'remark',
                    title: '备注'
                },
                {
                    field: 'createTime',
                    title: '创建时间',
					visible: false
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        //actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="updateNodeLoginCredentials(\'' + row.id + '\')"><i class="fa fa-key"></i>修改登录用户名密码</a>');
                        var more = [];
                        more.push("<a class='btn btn-default btn-xs' href='javascript:void(0)' onclick=updateNodeLoginCredentials('" + row.id + "')><i class='fa fa-key'></i>修改登录用户名密码</a> ");
                        actions.push('<a tabindex="0" class="btn btn-info btn-xs" role="button" data-container="body" data-placement="left" data-toggle="popover" data-html="true" data-trigger="hover" data-content="' + more.join('') + '"><i class="fa fa-chevron-circle-right"></i>更多操作</a>');
                        return actions.join('');
                    }
                }]
			};
			$.table.init(options);
		}

		/* 状态显示 */
		function statusTools(type, value, id) {
			let flag = value == 0;
			let method = type == 1 ? "SelfStart" : "Start";
			return '<i class="fa fa-toggle-{0} text-info fa-2x" onclick="{1}{2}(\'{3}\')"></i> '.format(flag ? "off" : "on", flag ? "enable" : "disable", method, id);
		}

		/* 监控-启用 */
		function enableStart(id) {
			$.modal.confirm("确认要启用监控吗？", function() {
				$.operate.post(prefix + "/changeStatus", { "id": id, "status": 1 });
			})
		}

		/* 监控-停用 */
		function disableStart(id) {
			$.modal.confirm("确认要关闭监控吗？", function() {
				$.operate.post(prefix + "/changeStatus", { "id": id, "status": 0 });
			})
		}

		/* 自启-启用 */
		function enableSelfStart(id) {
			$.modal.confirm("确认要启用自启吗？", function() {
				$.operate.post(prefix + "/changeSelfStatus", { "id": id, "selfStart": 1 });
			})
		}

		/* 自启-停用 */
		function disableSelfStart(id) {
			$.modal.confirm("确认要关闭自启吗？", function() {
				$.operate.post(prefix + "/changeSelfStatus", { "id": id, "selfStart": 0 });
			})
		}

		/* 修改登录凭证 */
		function updateNodeLoginCredentials(id) {
			var url = prefix + '/updateNodeLoginCredentials/' + id;
			$.modal.open("修改登录凭证", url, '800', '300');
		}
	</script>
</body>
</html>